<?php

declare(strict_types=1);

namespace app\admin\command;

use app\common\model\BasketballCompetitions;
use app\common\model\BasketballPlayerRank;
use app\common\model\BasketballTeamRank;
use app\common\model\FootballCompetitions;
use app\common\model\MatchesLeftCategory;
use app\common\model\MatchesSeason;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Team extends Command
{
    protected $model;
    protected function configure()
    {
        //足球篮球人员介绍 每周一次即可
        //       ->addOption('id', 'i', Option::VALUE_OPTIONAL, '数据库表的id')  此参数是数据量大了 导致抓取数据数据库断掉 增加的
        // 指令配置
        $this->setName('team')
            ->addArgument('action', Argument::OPTIONAL, '参数')
            ->addOption('type', 't', Option::VALUE_OPTIONAL, '区别 player 球员 team 球队')
            ->addOption('id', 'i', Option::VALUE_OPTIONAL, '数据库表的id') 
            ->setDescription('篮球球队,球员');
    }

    protected function execute(Input $input, Output $output)
    {
        $action = trim($input->getArgument('action'));
        $type = trim($input->getOption('type'));
        $id = trim($input->getOption('id'));
        if ($action === 'football') {
            $this->model = new FootballCompetitions();
        } else {
            $this->model = new BasketballCompetitions();
        }
        $left_cate = MatchesLeftCategory::where(['source' => $type, 'type' => $action])->select()->toArray();
        $this->model->where('id','>=',$id)->chunk(1, function ($models) use ($action, $output, $left_cate, $type) {
            foreach ($models as $model) {
                $seaon = MatchesSeason::field('season_id')->where(['competition_id' => $model['api_id'], 'type' => $action, 'is_current' => 1])->find(); //年份
                if (!empty($seaon)) {
                    foreach ($left_cate as $k => $point) {
                        $url = 'https://api-f.kqiu.cn/api/v1/' . $action . '/competition/' . $type . '-rank?competition_id=' . $model->api_id . '&season_id=' . $seaon["season_id"] . '&key=' . $point['key'];
                   
                        $res = curl_get($url);
                        $arr = json_decode($res, true);
                        if ($arr['code'] == 0) {
                            if (!empty($arr['data'])) {
                                $output->writeln('============进DATA============');
                                foreach ($arr['data'] as $k => $argv) {
                                    $argv['key'] = $point['key'];
                                    $argv['foreign_key_id'] = $model->id;
                                    $argv['api_id'] = $model->api_id;
                                    $argv['type']=$action;
                                    if ($type == 'team') {
                                        $output->writeln('============' . $model->id . '============');
                                        $is_find = BasketballTeamRank::where($argv)->find();
                                        if (empty($is_find)) {
                                            BasketballTeamRank::create($argv);
                                            $str = $type . "-add";
                                        } else {
                                            BasketballTeamRank::where('id', $is_find['id'])->update($argv);
                                            $str =  $type . '-update';
                                        }
                                    } else {
                                        $output->writeln('============' . $model->id . '============');
                                        $is_find = BasketballPlayerRank::where($argv)->find();
                                        if (empty($is_find)) {
                                            BasketballPlayerRank::create($argv);
                                            $str = $type . "-add";
                                        } else {
                                            BasketballPlayerRank::where('id', $is_find['id'])->update($argv);
                                            $str = $type . '-update';
                                        }
                                    }
                                    $output->writeln('==================循环' . $str . '完成====================');
                                }
                            }
                        }
                    }
                }
            }
        });
        // 指令输出
        $output->writeln('team');
    }
}
